<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <el-form-item label="退换货编号" prop="returnId">
        <el-input v-model="formData.returnId" placeholder="请输入退换货编号" />
      </el-form-item>
      <el-form-item label="退换货单号" prop="returnNo">
        <el-input v-model="formData.returnNo" placeholder="请输入退换货单号" />
      </el-form-item>
      <el-form-item label="订单项编号" prop="orderItemId">
        <el-input v-model="formData.orderItemId" placeholder="请输入订单项编号" />
      </el-form-item>
      <el-form-item label="SKU编号" prop="skuId">
        <el-input v-model="formData.skuId" placeholder="请输入SKU编号" />
      </el-form-item>
      <el-form-item label="SKU编码" prop="skuCode">
        <el-input v-model="formData.skuCode" placeholder="请输入SKU编码" />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="formData.productName" placeholder="请输入商品名称" />
      </el-form-item>
      <el-form-item label="退换货数量" prop="returnQuantity">
        <el-input v-model="formData.returnQuantity" placeholder="请输入退换货数量" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input v-model="formData.totalAmount" placeholder="请输入总金额" />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input v-model="formData.refundAmount" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="货币代码" prop="currencyCode">
        <el-input v-model="formData.currencyCode" placeholder="请输入货币代码" />
      </el-form-item>
      <el-form-item label="退换货原因" prop="returnReason">
        <el-input v-model="formData.returnReason" placeholder="请输入退换货原因" />
      </el-form-item>
      <el-form-item label="质量问题" prop="qualityIssue">
        <el-input v-model="formData.qualityIssue" placeholder="请输入质量问题" />
      </el-form-item>
      <el-form-item label="已收货数量" prop="receivedQuantity">
        <el-input v-model="formData.receivedQuantity" placeholder="请输入已收货数量" />
      </el-form-item>
      <el-form-item label="质检结果" prop="qualityCheckResult">
        <el-input v-model="formData.qualityCheckResult" placeholder="请输入质检结果" />
      </el-form-item>
      <el-form-item label="重新入库数量" prop="restockQuantity">
        <el-input v-model="formData.restockQuantity" placeholder="请输入重新入库数量" />
      </el-form-item>
      <el-form-item label="报废数量" prop="scrapQuantity">
        <el-input v-model="formData.scrapQuantity" placeholder="请输入报废数量" />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input v-model="formData.batchNo" placeholder="请输入批次号" />
      </el-form-item>
      <el-form-item label="序列号列表，逗号分隔" prop="serialNos">
        <el-input v-model="formData.serialNos" placeholder="请输入序列号列表，逗号分隔" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OrderReturnItemApi, OrderReturnItemVO } from '@/api/crossborder/orderreturnitem'

/** 跨境电商退换货明细 表单 */
defineOptions({ name: 'OrderReturnItemForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  returnId: undefined,
  returnNo: undefined,
  orderItemId: undefined,
  skuId: undefined,
  skuCode: undefined,
  productName: undefined,
  returnQuantity: undefined,
  unitPrice: undefined,
  totalAmount: undefined,
  refundAmount: undefined,
  currencyCode: undefined,
  returnReason: undefined,
  qualityIssue: undefined,
  receivedQuantity: undefined,
  qualityCheckResult: undefined,
  restockQuantity: undefined,
  scrapQuantity: undefined,
  batchNo: undefined,
  serialNos: undefined,
  remark: undefined,
})
const formRules = reactive({
  returnId: [{ required: true, message: '退换货编号不能为空', trigger: 'blur' }],
  returnNo: [{ required: true, message: '退换货单号不能为空', trigger: 'blur' }],
  orderItemId: [{ required: true, message: '订单项编号不能为空', trigger: 'blur' }],
  skuId: [{ required: true, message: 'SKU编号不能为空', trigger: 'blur' }],
  skuCode: [{ required: true, message: 'SKU编码不能为空', trigger: 'blur' }],
  productName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' }],
  returnQuantity: [{ required: true, message: '退换货数量不能为空', trigger: 'blur' }],
  unitPrice: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
  totalAmount: [{ required: true, message: '总金额不能为空', trigger: 'blur' }],
  refundAmount: [{ required: true, message: '退款金额不能为空', trigger: 'blur' }],
  currencyCode: [{ required: true, message: '货币代码不能为空', trigger: 'blur' }],
  receivedQuantity: [{ required: true, message: '已收货数量不能为空', trigger: 'blur' }],
  restockQuantity: [{ required: true, message: '重新入库数量不能为空', trigger: 'blur' }],
  scrapQuantity: [{ required: true, message: '报废数量不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderReturnItemApi.getOrderReturnItem(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OrderReturnItemVO
    if (formType.value === 'create') {
      await OrderReturnItemApi.createOrderReturnItem(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderReturnItemApi.updateOrderReturnItem(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    returnId: undefined,
    returnNo: undefined,
    orderItemId: undefined,
    skuId: undefined,
    skuCode: undefined,
    productName: undefined,
    returnQuantity: undefined,
    unitPrice: undefined,
    totalAmount: undefined,
    refundAmount: undefined,
    currencyCode: undefined,
    returnReason: undefined,
    qualityIssue: undefined,
    receivedQuantity: undefined,
    qualityCheckResult: undefined,
    restockQuantity: undefined,
    scrapQuantity: undefined,
    batchNo: undefined,
    serialNos: undefined,
    remark: undefined,
  }
  formRef.value?.resetFields()
}
</script>